package com.settlement.system.common.util;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils extends org.springframework.util.StringUtils {

    /**
     * 替换字符串
     * @param string 原字符串
     * @param preValue 需要替换的字符
     * @param finalValue 替换成的字符
     * @return 替换后的字符串
     */
    public static String replaceStr(String string, String preValue, String finalValue){
        if(!org.springframework.util.StringUtils.hasText(string)){
            return "";
        }
        if(!org.springframework.util.StringUtils.hasText(preValue)){
            return string;
        }
        if(!org.springframework.util.StringUtils.hasText(finalValue)){
            return string;
        }
        return string.replaceAll(preValue, finalValue);
    }

    public static String getString(String string, String defaultValue){
        if("null".equalsIgnoreCase(string)){
            return defaultValue;
        }
        if(Objects.isNull(string)){
            return defaultValue;
        }
        return string.strip();
    }

    private static Pattern linePattern = Pattern.compile("_(\\w)");
    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
